home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: 'Freeing' storage with realloc
- Date: 15 Feb 1996 13:34:19 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4fvcor$okv@sparcserver.lrz-muenchen.de>
- References: <4fv44f$p2v@news.rz.uni-passau.de>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
- Keywords: realloc, memory-managment
-
- berndl@sidonius.uni-passau.de (Klaus Berndl) writes:
-
-
- >Please look at the snipped of code:
-
- >#include <stdio.h>
-
- >main () {
-
- > char* buf = NULL;
-
- > buf = (char*)malloc(100);
-
- This is a real problem. There is no prototype for malloc() in your
- program, and your compiler cannot warn you that you are trying to
- assign an int to a char * because the cast hides that error.
-
- > strcpy(buf, "Klaus Berndl");
- > printf("\n%s\n", buf);
-
- >/* line 10 */ buf = (char*)realloc(buf, strlen(buf)+1); /* line 10 */
-
- > if (buf)
- > printf("\n%s\n", buf);
- > else
- > printf("\nshit!\n");
- >}
-
- >My questions are now: How much memory is allocated for 'buf' after
- >executing line 10?! Does realloc 'freeing' the storage behind byte 13?
- >Or are still 100 bytes allocated for buf?
-
- You cannot access buf[14] after line 10 in all implementations of C.
- Whether "freed" memory is available for further allocation requests
- of your program or other running programs depends on the implementation
- of your free storage management, so there is no general answer to
- this question. All reasonable implementations will at least make
- "freed" storage avaiable for further allocation requests from _your_
- program.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-